{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Linear and Geometric Algebra\n",
    "\n",
    "Walk through theorems and exercises in the book."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *\n",
    "from galgebra.printer import Format\n",
    "Format()\n",
    "from galgebra.ga import Ga"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chapter 1: Vectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "(x, y, z) = xyz = symbols('x,y,z',real=True)\n",
    "(o3d, ex, ey, ez) = Ga.build('e_x e_y e_z', g=[1, 1, 1], coords=xyz)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "V = o3d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = V.mv('a', 'scalar')\n",
    "b = V.mv('b', 'scalar')\n",
    "c = V.mv('c', 'scalar')\n",
    "d = V.mv('d', 'scalar')\n",
    "u = V.mv('u', 'vector')\n",
    "v = V.mv('v', 'vector')\n",
    "w = V.mv('w', 'vector')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def vector(ga, components):\n",
    "    bases = ga.mv()\n",
    "    return sum([components[i] * e for i, e in enumerate(bases)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Theorem 1.8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v + w == w + v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(u + v) + w == u + (v + w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v + S(0) == v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S(0) * v == S(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S(1) * v == v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * (b * v) == (a * b) * v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * (v + w) == a * v + a * w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(a + b) * v ==  a * v + b * v"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1.1.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "uu = vector(V, [1, 2, 3])\n",
    "vv = vector(V, [4, 5, 6])\n",
    "ww = vector(V, [5, 6, 7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} 5 \\boldsymbol{e}_{x} + 7 \\boldsymbol{e}_{y} + 9 \\boldsymbol{e}_{z} \\end{equation*}"
      ],
      "text/plain": [
       "5 \\boldsymbol{e}_{x} + 7 \\boldsymbol{e}_{y} + 9 \\boldsymbol{e}_{z}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "uu + vv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} 17 \\boldsymbol{e}_{x} + 26 \\boldsymbol{e}_{y} + 35 \\boldsymbol{e}_{z} \\end{equation*}"
      ],
      "text/plain": [
       "17 \\boldsymbol{e}_{x} + 26 \\boldsymbol{e}_{y} + 35 \\boldsymbol{e}_{z}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "7 * uu + 2 * ww"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} -3 \\boldsymbol{e}_{x} + 2 \\boldsymbol{e}_{y} + 7 \\boldsymbol{e}_{z} \\end{equation*}"
      ],
      "text/plain": [
       "-3 \\boldsymbol{e}_{x} + 2 \\boldsymbol{e}_{y} + 7 \\boldsymbol{e}_{z}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "7 * uu - 2 * ww"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} 16 \\boldsymbol{e}_{x} + 22 \\boldsymbol{e}_{y} + 28 \\boldsymbol{e}_{z} \\end{equation*}"
      ],
      "text/plain": [
       "16 \\boldsymbol{e}_{x} + 22 \\boldsymbol{e}_{y} + 28 \\boldsymbol{e}_{z}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 * uu + 2 * vv + ww"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problems 1.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*}  0  \\end{equation*}"
      ],
      "text/plain": [
       " 0 "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v + S(-1) * v"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chapter 2: Vector Spaces\n",
    "\n",
    "### Exercise 2.7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "v0 = vector(V, [0, 0, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * v0 == v0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2.1.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(-a) * v == a * (-v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(-a) * v == - a * v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\left ( \\sqrt{2} u^{x} + \\frac{2 v^{x}}{3}\\right ) \\boldsymbol{e}_{x} + \\left ( \\sqrt{2} u^{y} + \\frac{2 v^{y}}{3}\\right ) \\boldsymbol{e}_{y} + \\left ( \\sqrt{2} u^{z} + \\frac{2 v^{z}}{3}\\right ) \\boldsymbol{e}_{z} \\end{equation*}"
      ],
      "text/plain": [
       "\\left ( \\sqrt{2} u^{x} + \\frac{2 v^{x}}{3}\\right ) \\boldsymbol{e}_{x} + \\left ( \\sqrt{2} u^{y} + \\frac{2 v^{y}}{3}\\right ) \\boldsymbol{e}_{y} + \\left ( \\sqrt{2} u^{z} + \\frac{2 v^{z}}{3}\\right ) \\boldsymbol{e}_{z}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sqrt(2) * u + Rational(2, 3) * v"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chapter 3 Matrices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}2 & -1 & 1\\\\0 & -2 & 5\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{ccc} 2 & -1 & 1  \\\\ 0 & -2 & 5  \\end{array}\\right ] "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MA = Matrix([[2, -1, 1], [0, -2, 5]])\n",
    "MA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}2 & 3\\\\0 & -1\\\\1 & -3\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} 2 & 3  \\\\ 0 & -1  \\\\ 1 & -3  \\end{array}\\right ] "
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MB = Matrix([[2, 3], [0, -1], [1, -3]])\n",
    "MB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}5 & 4\\\\5 & -13\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} 5 & 4  \\\\ 5 & -13  \\end{array}\\right ] "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MAB = MA * MB\n",
    "MAB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}5\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{c} 5  \\end{array}\\right ] "
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MAB_00 = MA[0, :] * MB[:, 0]\n",
    "MAB_00"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\frac{13}{85} & \\frac{4}{85}\\\\\\frac{1}{17} & - \\frac{1}{17}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} \\frac{13}{85} & \\frac{4}{85}  \\\\ \\frac{1}{17} & - \\frac{1}{17}  \\end{array}\\right ] "
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MAB.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}-13 & -4\\\\-5 & 5\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} -13 & -4  \\\\ -5 & 5  \\end{array}\\right ] "
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MAB.inv() * MAB.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 0\\\\0 & 1\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} 1 & 0  \\\\ 0 & 1  \\end{array}\\right ] "
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MAB.inv() * MAB"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3.11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a & b\\\\c & d\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} a & b  \\\\ c & d  \\end{array}\\right ] "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a, b, c, d = symbols('a b c d')\n",
    "M = Matrix([[a, b], [c, d]])\n",
    "M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$a d - b c$$"
      ],
      "text/plain": [
       "a⋅d - b⋅c"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\frac{d}{a d - b c} & - \\frac{b}{a d - b c}\\\\- \\frac{c}{a d - b c} & \\frac{a}{a d - b c}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} \\frac{d}{a d - b c} & - \\frac{b}{a d - b c}  \\\\ - \\frac{c}{a d - b c} & \\frac{a}{a d - b c}  \\end{array}\\right ] "
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\frac{\\left[\\begin{matrix}d & - b\\\\- c & a\\end{matrix}\\right]}{a d - b c}$$"
      ],
      "text/plain": [
       " ⎡d   -b⎤\n",
       " ⎢      ⎥\n",
       " ⎣-c  a ⎦\n",
       "─────────\n",
       "a⋅d - b⋅c"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Mul(1 / M.det(), M.inv() * M.det())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Theorem 3.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "a, b, c, d, e, f, g, h = symbols('a b c d e f g h')\n",
    "A = Matrix([[a, b], [c, d]])\n",
    "B =Matrix([[e, f], [g, h]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a e + b g & a f + b h\\\\c e + d g & c f + d h\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} a e + b g & a f + b h  \\\\ c e + d g & c f + d h  \\end{array}\\right ] "
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A*B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\frac{\\left(a e + b g\\right) \\left(c f + d h\\right) - \\left(a f + b h\\right) \\left(- c e - d g\\right) - \\left(a f + b h\\right) \\left(c e + d g\\right)}{\\left(a e + b g\\right) \\left(\\left(a e + b g\\right) \\left(c f + d h\\right) - \\left(a f + b h\\right) \\left(c e + d g\\right)\\right)} & - \\frac{a f + b h}{\\left(a e + b g\\right) \\left(c f + d h\\right) - \\left(a f + b h\\right) \\left(c e + d g\\right)}\\\\\\frac{- c e - d g}{\\left(a e + b g\\right) \\left(c f + d h\\right) - \\left(a f + b h\\right) \\left(c e + d g\\right)} & \\frac{a e + b g}{\\left(a e + b g\\right) \\left(c f + d h\\right) - \\left(a f + b h\\right) \\left(c e + d g\\right)}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} \\frac{\\left(a e + b g\\right) \\left(c f + d h\\right) - \\left(a f + b h\\right) \\left(- c e - d g\\right) - \\left(a f + b h\\right) \\left(c e + d g\\right)}{\\left(a e + b g\\right) \\left(\\left(a e + b g\\right) \\left(c f + d h\\right) - \\left(a f + b h\\right) \\left(c e + d g\\right)\\right)} & - \\frac{a f + b h}{\\left(a e + b g\\right) \\left(c f + d h\\right) - \\left(a f + b h\\right) \\left(c e + d g\\right)}  \\\\ \\frac{- c e - d g}{\\left(a e + b g\\right) \\left(c f + d h\\right) - \\left(a f + b h\\right) \\left(c e + d g\\right)} & \\frac{a e + b g}{\\left(a e + b g\\right) \\left(c f + d h\\right) - \\left(a f + b h\\right) \\left(c e + d g\\right)}  \\end{array}\\right ] "
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(A * B).inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\frac{c f}{\\left(a d - b c\\right) \\left(e h - f g\\right)} + \\frac{d h}{\\left(a d - b c\\right) \\left(e h - f g\\right)} & - \\frac{a f}{\\left(a d - b c\\right) \\left(e h - f g\\right)} - \\frac{b h}{\\left(a d - b c\\right) \\left(e h - f g\\right)}\\\\- \\frac{c e}{\\left(a d - b c\\right) \\left(e h - f g\\right)} - \\frac{d g}{\\left(a d - b c\\right) \\left(e h - f g\\right)} & \\frac{a e}{\\left(a d - b c\\right) \\left(e h - f g\\right)} + \\frac{b g}{\\left(a d - b c\\right) \\left(e h - f g\\right)}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} \\frac{c f}{\\left(a d - b c\\right) \\left(e h - f g\\right)} + \\frac{d h}{\\left(a d - b c\\right) \\left(e h - f g\\right)} & - \\frac{a f}{\\left(a d - b c\\right) \\left(e h - f g\\right)} - \\frac{b h}{\\left(a d - b c\\right) \\left(e h - f g\\right)}  \\\\ - \\frac{c e}{\\left(a d - b c\\right) \\left(e h - f g\\right)} - \\frac{d g}{\\left(a d - b c\\right) \\left(e h - f g\\right)} & \\frac{a e}{\\left(a d - b c\\right) \\left(e h - f g\\right)} + \\frac{b g}{\\left(a d - b c\\right) \\left(e h - f g\\right)}  \\end{array}\\right ] "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B.inv() * A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify((A * B).inv()) == simplify(B.inv() * A.inv())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0\\\\0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0⎤\n",
       "⎢    ⎥\n",
       "⎣0  0⎦"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify((A * B).inv() - B.inv() * A.inv())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Corollary 3.8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.inv().inv() == A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0\\\\0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡0  0⎤\n",
       "⎢    ⎥\n",
       "⎣0  0⎦"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(A.inv().inv() - A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3.19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = Matrix([\n",
    "    [1, -4],\n",
    "    [1, -1]\n",
    "])\n",
    "b = Matrix([3, -1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & -4\\\\1 & -1\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} 1 & -4  \\\\ 1 & -1  \\end{array}\\right ] "
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}3\\\\-1\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{c} 3  \\\\ -1  \\end{array}\\right ] "
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}- \\frac{7}{3}\\\\- \\frac{4}{3}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{c} - \\frac{7}{3}  \\\\ - \\frac{4}{3}  \\end{array}\\right ] "
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.LUsolve(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chapter 4 Inner Product Spaces"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Theorem 4 .2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\frac{u^{x} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{x} + \\frac{u^{y} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{y} + \\frac{u^{z} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{z} \\end{equation*}"
      ],
      "text/plain": [
       "\\frac{u^{x} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{x} + \\frac{u^{y} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{y} + \\frac{u^{z} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{z}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v.project_in_blade(u)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\frac{u^{x} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{x} + \\frac{u^{y} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{y} + \\frac{u^{z} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{z} \\end{equation*}"
      ],
      "text/plain": [
       "\\frac{u^{x} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{x} + \\frac{u^{y} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{y} + \\frac{u^{z} \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)}{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\boldsymbol{e}_{z}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(u|v)/u**2*u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v.project_in_blade(u) == (u|v)/u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v.project_in_blade(w) + u.project_in_blade(w) == (u + v).project_in_blade(w)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Theorem 4.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(a * u) | v == a * (u | v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(u + v) | w == (u | w) + (v | w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u | v == v | u"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Definition 4 .11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v | v == v.norm2()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4 .16\n",
    "\n",
    "parallelogram identity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(u + v).norm2() + (u - v).norm2() == 2 * u.norm2() + 2 * v.norm2()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Lemma 4.13 (Cauchy-Schwarz inequality)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)^{1.0} \\leq \\sqrt{\\left(u^{x}\\right)^{2} + \\left(u^{y}\\right)^{2} + \\left(u^{z}\\right)^{2}} \\sqrt{\\left(v^{x}\\right)^{2} + \\left(v^{y}\\right)^{2} + \\left(v^{z}\\right)^{2}}$$"
      ],
      "text/plain": [
       "                                            _______________________    _______\n",
       "                                   1.0     ╱     2       2       2    ╱     2 \n",
       "(u__x⋅v__x + u__y⋅v__y + u__z⋅v__z)    ≤ ╲╱  u__x  + u__y  + u__z  ⋅╲╱  v__x  \n",
       "\n",
       "________________\n",
       "      2       2 \n",
       "+ v__y  + v__z  "
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(u | v).norm() <= u.norm() * v.norm()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\sqrt{\\left(u^{x}\\right)^{2} + \\left(u^{y}\\right)^{2} + \\left(u^{z}\\right)^{2}} \\sqrt{\\left(v^{x}\\right)^{2} + \\left(v^{y}\\right)^{2} + \\left(v^{z}\\right)^{2}} - \\left(u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}\\right)^{1.0}$$"
      ],
      "text/plain": [
       "   _______________________    _______________________                         \n",
       "  ╱     2       2       2    ╱     2       2       2                          \n",
       "╲╱  u__x  + u__y  + u__z  ⋅╲╱  v__x  + v__y  + v__z   - (u__x⋅v__x + u__y⋅v__y\n",
       "\n",
       "                \n",
       "             1.0\n",
       " + u__z⋅v__z)   "
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(u.norm() * v.norm() - (u | v).norm())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Definition 4.15 (Angle between vectors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cosv(u, v):\n",
    "    return (u | v) / (u.norm() * v.norm())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\frac{u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}}{\\sqrt{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\sqrt{{\\left ( v^{x} \\right )}^{2} + {\\left ( v^{y} \\right )}^{2} + {\\left ( v^{z} \\right )}^{2}}} \\end{equation*}"
      ],
      "text/plain": [
       "\\frac{u^{x} v^{x} + u^{y} v^{y} + u^{z} v^{z}}{\\sqrt{{\\left ( u^{x} \\right )}^{2} + {\\left ( u^{y} \\right )}^{2} + {\\left ( u^{z} \\right )}^{2}} \\sqrt{{\\left ( v^{x} \\right )}^{2} + {\\left ( v^{y} \\right )}^{2} + {\\left ( v^{z} \\right )}^{2}}}"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cosv(u, v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4.32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "VO4 = Ga('e', g=[1, 1, 1, 1], coords=symbols('v1,v2,v3,v4',real=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "vo4 = vector(VO4, [1, 1, 1, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*}  \\boldsymbol{e}_{v1} + \\boldsymbol{e}_{v2} + \\boldsymbol{e}_{v3} + \\boldsymbol{e}_{v4} \\end{equation*}"
      ],
      "text/plain": [
       " \\boldsymbol{e}_{v1} + \\boldsymbol{e}_{v2} + \\boldsymbol{e}_{v3} + \\boldsymbol{e}_{v4}"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vo4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "e_u1 = vector(VO4, [3, 0, 4, 0])\n",
    "e_u2 = vector(VO4, [0, 3, 0, 4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} 3 \\boldsymbol{e}_{v1} + 4 \\boldsymbol{e}_{v3} \\end{equation*}"
      ],
      "text/plain": [
       "3 \\boldsymbol{e}_{v1} + 4 \\boldsymbol{e}_{v3}"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "e_u1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} 3 \\boldsymbol{e}_{v2} + 4 \\boldsymbol{e}_{v4} \\end{equation*}"
      ],
      "text/plain": [
       "3 \\boldsymbol{e}_{v2} + 4 \\boldsymbol{e}_{v4}"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "e_u2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "u1, u2 = symbols('u1,u2',real=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} 3 u_{1} \\boldsymbol{e}_{v1} + 3 u_{2} \\boldsymbol{e}_{v2} + 4 u_{1} \\boldsymbol{e}_{v3} + 4 u_{2} \\boldsymbol{e}_{v4} \\end{equation*}"
      ],
      "text/plain": [
       "3 u_{1} \\boldsymbol{e}_{v1} + 3 u_{2} \\boldsymbol{e}_{v2} + 4 u_{1} \\boldsymbol{e}_{v3} + 4 u_{2} \\boldsymbol{e}_{v4}"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coord_fun = u1 * e_u1 + u2 * e_u2\n",
    "coord_fun"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left [ 3 u_{1}, \\quad 3 u_{2}, \\quad 4 u_{1}, \\quad 4 u_{2}\\right ]$$"
      ],
      "text/plain": [
       "[3⋅u₁, 3⋅u₂, 4⋅u₁, 4⋅u₂]"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coord_map = [(coord_fun | e).obj for e in VO4.mv()]\n",
    "coord_map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<galgebra.ga.Sm at 0x10ebe9d30>"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "UO2 = VO4.sm(coord_map, [u1, u2])\n",
    "UO2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left [ 3 u_{1}, \\quad 3 u_{2}, \\quad 4 u_{1}, \\quad 4 u_{2}\\right ]$$"
      ],
      "text/plain": [
       "[3⋅u₁, 3⋅u₂, 4⋅u₁, 4⋅u₂]"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "UO2.u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}25 & 0\\\\0 & 25\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       " \\left [ \\begin{array}{cc} 25 & 0  \\\\ 0 & 25  \\end{array}\\right ] "
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "UO2.g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} 9 \\boldsymbol{e}_{v1}\\wedge \\boldsymbol{e}_{v2} + 12 \\boldsymbol{e}_{v1}\\wedge \\boldsymbol{e}_{v4} -12 \\boldsymbol{e}_{v2}\\wedge \\boldsymbol{e}_{v3} + 16 \\boldsymbol{e}_{v3}\\wedge \\boldsymbol{e}_{v4} \\end{equation*}"
      ],
      "text/plain": [
       "9 \\boldsymbol{e}_{v1}\\wedge \\boldsymbol{e}_{v2} + 12 \\boldsymbol{e}_{v1}\\wedge \\boldsymbol{e}_{v4} -12 \\boldsymbol{e}_{v2}\\wedge \\boldsymbol{e}_{v3} + 16 \\boldsymbol{e}_{v3}\\wedge \\boldsymbol{e}_{v4}"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "UO2E = e_u1 ^ e_u2\n",
    "UO2E"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\frac{17}{25} \\boldsymbol{e}_{v1} + \\frac{17}{25} \\boldsymbol{e}_{v2} + \\frac{31}{25} \\boldsymbol{e}_{v3} + \\frac{31}{25} \\boldsymbol{e}_{v4} \\end{equation*}"
      ],
      "text/plain": [
       "\\frac{17}{25} \\boldsymbol{e}_{v1} + \\frac{17}{25} \\boldsymbol{e}_{v2} + \\frac{31}{25} \\boldsymbol{e}_{v3} + \\frac{31}{25} \\boldsymbol{e}_{v4}"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vo4.reflect_in_blade(UO2E)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
